package org.yray.sip.rtp.dtmf;


import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import lombok.extern.slf4j.Slf4j;
import org.yray.sip.constant.Constants;

import java.util.concurrent.TimeUnit;

@Slf4j
public class DtmfContext {
    private static Cache<String,StringBuffer> dtmfCache = Caffeine.newBuilder()
            .maximumSize(2048)
            .expireAfterWrite(Constants.CACHE_TIME, TimeUnit.SECONDS)
            .removalListener((RemovalListener<String, StringBuffer>) (key, value, cause) ->
                    log.info("dtmfCache,key:" + key + ", value:" + value + ", 删除原因:" + cause.toString()))
            .build();

    public static void putDtmf(String callId,byte[] bs){
        int d=(int)bs[12];
        StringBuffer sb= dtmfCache.get(callId,v->new StringBuffer());
        if(d>9)
        {
            log.info("dtmf=[{}]",sb.toString());
            dtmfCache.invalidate(callId);
        }

        else {
            sb.append(d);
        }
    }
}
